package weiy.top.modules.purchase.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import weiy.top.constant.WareConstant;
import weiy.top.modules.purchase.entity.WmsPurchase;
import weiy.top.modules.purchase.entity.WmsPurchaseDetail;
import weiy.top.modules.purchase.mapper.WmsPurchaseMapper;
import weiy.top.modules.purchase.service.IWmsPurchaseDetailService;
import weiy.top.modules.purchase.service.IWmsPurchaseService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import weiy.top.modules.purchase.vo.MergeVo;
import weiy.top.modules.purchase.vo.PurchaseDoneVo;
import weiy.top.modules.purchase.vo.PurchaseItemDoneVo;
import weiy.top.modules.sku.service.IWmsWareSkuService;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: wms_purchase
 * @Author: weihancheng
 * @Date:   2021-12-14
 * @Version: V1.0
 */
@Service
public class WmsPurchaseServiceImpl extends ServiceImpl<WmsPurchaseMapper, WmsPurchase> implements IWmsPurchaseService {

    @Autowired
    private IWmsPurchaseDetailService purchaseDetailService;
    @Autowired
    private IWmsWareSkuService wareSkuService;

    @Override
    public void mergePurchase(MergeVo mergeVo) {
        String purchaseId = mergeVo.getPurchaseId();
        if (StringUtils.isEmpty(purchaseId)) {
            WmsPurchase purchase = new WmsPurchase();
            // 新建的采购单状态设置为创建
            purchase.setStatus(WareConstant.PurchaseDetailStatusEnum.CREATED.getCode());
            this.save(purchase);
            purchaseId = purchase.getId();
        }

        List<String> items = mergeVo.getItems();

        String finalPurchaseId = purchaseId;
        List<WmsPurchaseDetail> detailList = items.stream().map(i -> {
            WmsPurchaseDetail purchaseDetail = new WmsPurchaseDetail();
            purchaseDetail.setPurchaseId(finalPurchaseId);
            purchaseDetail.setStatus(WareConstant.PurchaseDetailStatusEnum.ASSIGNED.getCode());
            return purchaseDetail;
        }).collect(Collectors.toList());

        purchaseDetailService.updateBatchById(detailList);

        // 这里触发修改update_time
        WmsPurchase purchaseEntity = new WmsPurchase();
        purchaseEntity.setId(purchaseId);
        this.updateById(purchaseEntity);
    }

    @Override
    public void received(List<Long> ids) {

    }

    @Override
    public void receivePurchases(List<String> purchaseIds) {
        // 更新采购单的信息状态
        List<WmsPurchase> purchaseList = purchaseIds.stream().map(this::getById).filter(item -> {
            // 过滤采购单 确认采购单的状态为 新建、已分配状态
            Integer status = item.getStatus();
            return status != null && (status == WareConstant.PurchaseStatusEnum.CREATED.getCode() || status == WareConstant.PurchaseStatusEnum.ASSIGNED.getCode());
        }).peek(item ->
                item.setStatus(WareConstant.PurchaseStatusEnum.RECEIVE.getCode())
        ).collect(Collectors.toList());

       this.saveBatch(purchaseList);

       // 更新采购单的需求状态
       purchaseList.forEach(item -> {
           List<WmsPurchaseDetail> purchaseDetailEntities = purchaseDetailService.getPurchaseDetailByPurchaseId(item.getId());

           // 只更新两个属性
           List<WmsPurchaseDetail> collect = purchaseDetailEntities.stream().map(entity -> {
               WmsPurchaseDetail purchaseDetailEntity = new WmsPurchaseDetail();
               purchaseDetailEntity.setId(entity.getId());
               purchaseDetailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.BUYING.getCode());
               return purchaseDetailEntity;
           }).collect(Collectors.toList());

           purchaseDetailService.updateBatchById(collect);
       });
    }

    /**
     * 员工完成采购单
     * @param doneVo
     */
    @Transactional
    @Override
    public void donePurchases(PurchaseDoneVo doneVo) {
        String id = doneVo.getId();
        boolean flag = true;
        List<PurchaseItemDoneVo> items = doneVo.getItems();

        ArrayList<WmsPurchaseDetail> purchaseDetails = new ArrayList<>(items.size());
        for (PurchaseItemDoneVo item : items) {
            WmsPurchaseDetail detailEntity = new WmsPurchaseDetail();
            if (WareConstant.PurchaseDetailStatusEnum.HAS_ERROR.getCode() == item.getStatus()) {
                flag = false;
                detailEntity.setStatus(item.getStatus());
            } else {
                detailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.FINISH.getCode());

                WmsPurchaseDetail entity = purchaseDetailService.getById(item.getItemId());
                // 商品入库
                wareSkuService.addStock(entity.getSkuId(), entity.getWareId(), entity.getSkuNum());
            }
            detailEntity.setId(item.getItemId());
            purchaseDetails.add(detailEntity);
        }

        purchaseDetailService.updateBatchById(purchaseDetails);

        //1、改变采购单状态
        WmsPurchase purchaseEntity = new WmsPurchase();
        purchaseEntity.setId(id);
        purchaseEntity.setStatus(flag ? WareConstant.PurchaseStatusEnum.FINISH.getCode() : WareConstant.PurchaseStatusEnum.HAS_ERROR.getCode());
        updateById(purchaseEntity);
    }
}
